/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package percussiongenerator.input;

import java.util.ArrayList;
import org.jfonia.model.PerformanceNode;
import org.jfonia.model.Staff;
import org.jfonia.model.ToneSequence;

/**
 *
 * @author Jannes Plyson
 */
public class InputNotes {
    ArrayList<SimpleNote> notes;

    public InputNotes(){
        notes = new ArrayList<SimpleNote>();
    }    

    public Staff getStaff(){
        Staff staff = new Staff();
        ToneSequence seq = new ToneSequence();
        staff.addSequence(seq);
        if(!notes.isEmpty()){
            SimpleNote sn = notes.get(0);
            long start = sn.start;
            PerformanceNode node = new PerformanceNode(sn.end-sn.start);
            node.setPitch(sn.pitch);
            node.setVelocity(sn.velocity);
            seq.add(node);
            for(int i = 1; i < notes.size(); i++){
                sn = notes.get(i);
                if(seq.getPerformanceLength() < sn.start - start){
                    PerformanceNode silence = new PerformanceNode(sn.start-start-seq.getPerformanceLength());
                    silence.setPitch(0);
                    silence.setVelocity(0);
                    seq.add(silence);
                }
                node = new PerformanceNode(sn.end-sn.start);
                node.setPitch(sn.pitch);
                node.setVelocity(sn.velocity);
                seq.add(node);
            }
        }
        seq.setLockNotation(false);
        return staff;
    }

    public void addInput(long nanoTimeStart, long nanoTimeEnd, int pitch, int velocity){        
        notes.add(new SimpleNote(nanoTimeStart, nanoTimeEnd, pitch, velocity));
    }

    public void addInput(double secondsStart, double secondsEnd,int pitch, int velocity){
        long nanoTimeStart = (long)(secondsStart*1000000000);
        long nanoTimeEnd = (long)(secondsEnd*1000000000);
        addInput(nanoTimeStart,nanoTimeEnd,pitch,velocity);
    }

    private class SimpleNote{
        public long start;
        public long end;
        public int pitch;
        public int velocity;

        public SimpleNote(long start, long end, int pitch, int velocity) {
            this.start = start;
            this.end = end;
            this.pitch = pitch;
            this.velocity = velocity;
        }
    }

}
